package com.premadefood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.premadefood.common.BaseResponse;
import com.premadefood.common.ErrorCode;
import com.premadefood.domain.Goods;
import com.premadefood.domain.dto.GoodsDTO;
import com.premadefood.domain.vo.GoodsVO;
import com.premadefood.exception.BusinessException;
import com.premadefood.service.GoodsService;
import com.premadefood.service.UserService;
import com.premadefood.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private UserService userService;



    /**
     * 进入商城页面展示商品
     * @return
     */
    @GetMapping("/index")
    public BaseResponse<Page<Goods>> showGoods(long pageNum){
        if(pageNum < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"页数应为正数");
        }
        Page<Goods> page = goodsService.page(new Page<>(pageNum, 15));
        return ResultUtils.success(page);
    }

    /**
     * 点击分类按钮后返回分类后的各商品信息
     * @return
     */
    //todo 此处的类别和pageNum需要封装为一个Request类，看后期测试时能否正确传参
    @GetMapping("/category")
    public BaseResponse<Page<Goods>> classifyGoods(String categoryName,long pageNum){
        if(StringUtils.isBlank(categoryName)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"类别名不能为空");
        }
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",categoryName);

        //todo 此处在想是不是应该做一个下拉显示更多商品？还是直接粗暴地分页，并且应该写在service层中
        Page<Goods> page = goodsService.page(new Page<>(pageNum, 15), queryWrapper);
        return ResultUtils.success(page);
    }

    /**
     * 搜索商品
     *
     */
    @GetMapping("/search")
    public BaseResponse<List<GoodsVO>> searchGoods(String searchContext){
        if(StringUtils.isBlank(searchContext)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"商品搜索内容不能为空");
        }
        List<GoodsVO> goodsList =goodsService.searchGoods(searchContext);
        return ResultUtils.success(goodsList);
    }

    /**
     * 查看商品详情
     *
     */
    @GetMapping("/detail")
    public BaseResponse<GoodsDTO> showGoodsDetail(Integer goodsId, HttpServletRequest request){
        if(goodsId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"商品id应为正数");
        }
        GoodsDTO goodsDTO = goodsService.showDetail(goodsId, request);
        return ResultUtils.success(goodsDTO);
    }

    /**
     * 收藏商品
     * @param goodsId
     * @param request
     * @return
     */

    @PostMapping("/favorite/goods")
    public BaseResponse<Boolean> favoriteGoods(Integer goodsId, HttpServletRequest request){
        if(goodsId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"商品id应为正数");
        }

        Boolean save = goodsService.focusGoods(goodsId, request);
        return ResultUtils.success(save);
    }

    /**
     * 收藏店铺
     * @param storeName
     * @param request
     * @return
     */
    @PostMapping("/favorite/store")
    public BaseResponse<Boolean> favoriteGoods(String storeName, HttpServletRequest request){
        if(StringUtils.isBlank(storeName)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"商品名称不能为空");
        }

        Boolean save = goodsService.focusStore(storeName, request);
        return ResultUtils.success(save);
    }

    /**
     * 点击时令上新展示时令上新商品
     */
    @GetMapping("/timenew")
    public BaseResponse<List<GoodsVO>> showTimeNew(long pageNum){
        List<GoodsVO> goodsVOList = goodsService.showTimeNew(pageNum);
        return ResultUtils.success(goodsVOList);
    }

    /**
     * 点击秒杀特卖展示秒杀特卖商品
     */
    @GetMapping("/sale")
    public BaseResponse<List<GoodsVO>> showSale(long pageNum){
        List<GoodsVO> goodsVOList = goodsService.showSecondSale(pageNum);
        return ResultUtils.success(goodsVOList);
    }

}
